//
//  FR_VisitortViewController.m
//  FaceRecognition
//
//  Created by zz on 17/7/5.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_VisitortViewController.h"
#import "FR_Visitor_TableViewCell.h"
#import "Visitor_waitHandleView.h"
#import "Visitor_hasHandledView.h"

#import "FR_VisitorModel.h"

@interface FR_VisitortViewController ()<UITableViewDataSource,UITableViewDelegate,Visitor_waitHandleDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UILabel *emptyLabel;

@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) NSInteger pageSize;



@end

@implementation FR_VisitortViewController

#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight ) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self requestList];
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
           
            [self requestMoreList];
        }];
        
        _tableView.rowHeight = 108;
        
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, VIEW_WIDTH, 15)];
        _emptyLabel.font = [UIFont systemFontOfSize:15];
        _emptyLabel.text = @"暂无数据";
        _emptyLabel.textColor = [UIColor colorWithHexString:@"#979797"];
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        _emptyLabel.hidden = YES;
        [_tableView addSubview:_emptyLabel];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArr {
    
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
    }
    
    return _dataArr;
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = YES;
    self.navBar.nav_titleLabel.text = @"访客列表";
    [self.view addSubview:self.tableView];
    

    [self.tableView.mj_header beginRefreshing];
    
    
}


#pragma mark -Request 


- (void)requestList {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:USERID forKey:@"intervieweeid"];
    [dict setObject:@"1" forKey:@"pageno"];
    [dict setObject:@"20" forKey:@"pagesize"];
    [dict setObject:SCHOOLID forKey:@"scid"];
    
    [Utils Java_get:@"api/getVisitorList" parameters:dict success:^(NSInteger recode, id jsonObject) {
        _pageNo = 1;
        [self.dataArr removeAllObjects];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count < 20) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
         
            _pageNo++;
            [self.tableView.mj_footer endRefreshing];
        }
        
        self.emptyLabel.hidden = arr.count != 0;
        
        self.dataArr = [FR_VisitorModel mj_objectArrayWithKeyValuesArray:arr];
        
        [self.tableView.mj_header endRefreshing];
        
        [self.tableView reloadData];
        
        if (_visitId) {
            //获取详情记录
            [self requestDetail];
        }
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
}

- (void)requestMoreList {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:USERID forKey:@"intervieweeid"];
    [dict setObject:@(_pageNo) forKey:@"pageno"];
    [dict setObject:@"20" forKey:@"pagesize"];
    [dict setObject:SCHOOLID forKey:@"scid"];
    
    [Utils Java_get:@"api/getVisitorList" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count < 20) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            
            _pageNo++;
            [self.tableView.mj_footer endRefreshing];
        }
        
        NSArray *arrM = [FR_VisitorModel mj_objectArrayWithKeyValuesArray:arr];
        [self.dataArr addObjectsFromArray:arrM];
        [self.tableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
}

- (void)requestDetail {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:USERID forKey:@"intervieweeid"];
    [dict setObject:_visitId forKey:@"visitorid"];
    
    [Utils Java_get:@"pad/getVisitorDetail" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        //请求成功 清空_visitid
        _visitId = nil;
        
        NSDictionary *re_data = [jsonObject objectForKey:@"re_data"];
        
        FR_VisitorModel *model = [FR_VisitorModel mj_objectWithKeyValues:re_data];
        
        if (model.approval == 0) { //未处理
            
            //未处理
            Visitor_waitHandleView *waitView = [[Visitor_waitHandleView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            waitView.model = model;
            waitView.delegate = self;
            [self.view addSubview:waitView];
            
            
        }else {
            
            //已处理
            Visitor_hasHandledView *hasHandleView = [[Visitor_hasHandledView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            hasHandleView.model = model;
            [self.view addSubview:hasHandleView];
        }

        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];

}

#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellid = @"cellid";
    
    FR_Visitor_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    
    if (!cell) {
        
        cell = [[FR_Visitor_TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
    }
    
    FR_VisitorModel *model = [self.dataArr objectAtIndex:indexPath.row];
    
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    FR_VisitorModel *model = [self.dataArr objectAtIndex:indexPath.row];
    
    if (model.approval == 0) { //未处理
        
        //未处理
        Visitor_waitHandleView *waitView = [[Visitor_waitHandleView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        waitView.model = model;
        waitView.delegate = self;
        [self.view addSubview:waitView];
        
        
    }else {
        
        //已处理
        Visitor_hasHandledView *hasHandleView = [[Visitor_hasHandledView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        hasHandleView.model = model;
        [self.view addSubview:hasHandleView];
    }
    
}


#pragma mark -Visitor_waitHandleDelegate
- (void)handledAction:(FR_VisitorModel *)model {
    
    if (self.dataArr.count > 0) {
        
        NSInteger index = [self.dataArr indexOfObject:model];
        
        if (index > 1000000) { //没有找到index
            
            for (NSInteger i = 0; i < self.dataArr.count; i++) {
                
                FR_VisitorModel *listmodel = [self.dataArr objectAtIndex:i];
                
                if (listmodel.facevisitorid == model.facevisitorid) {
                    model.intervieweename = listmodel.intervieweename;
                    index = i;
                    break;
                }
            }
        }
        
        
        if (index < 100000) {
            
            [self.dataArr replaceObjectAtIndex:index withObject:model];
            [self.tableView reloadData];
        }
        
    }
}

@end
